package org.ymx.myMap;


import java.util.Arrays;

public class MyHashTable<K, V> {

    private MyEntry<K, V>[] data = (MyEntry[]) new Object[16];

    public void put(K key, V val) {
        //调用散列函数获取存储位置
        int index = key.hashCode();
        //添加元素
        data[index].setKey(key);
        data[index].setVal(val);
    }

    public MyEntry get(MyEntry item) {
        return data[item.getHash()];
    }

    @Override
    public String toString() {
        return "HashTable [data=" + Arrays.toString(data) + "]";
    }


    /**
     * 内部静态类
     *
     * @param <K>
     * @param <V>
     */
    static class MyEntry<K, V> {
        private int hash;
        private K key;
        private V val;
        MyEntry<K, V> next;

        public MyEntry(int hash, K key, V val, MyEntry<K, V> next) {
            this.hash = hash;
            this.key = key;
            this.val = val;
            this.next = next;
        }

        public int getHash() {
            return hash;
        }

        public void setHash(int hash) {
            this.hash = hash;
        }

        public K getKey() {
            return key;
        }

        public void setKey(K key) {
            this.key = key;
        }

        public V getVal() {
            return val;
        }

        public void setVal(V val) {
            this.val = val;
        }

        public MyEntry<K, V> getNext() {
            return next;
        }

        public void setNext(MyEntry<K, V> next) {
            this.next = next;
        }
    }
}
